<?php

namespace app\api\controller;

use app\api\model\Information;
use app\api\model\Search as ModelSearch;
use app\api\validate\Information as Vinfrom;
use app\api\validate\Search;
use app\common\controller\Api;
use think\Cache;
use think\Db;

/**
 * 首页接口
 */
class Index extends Api
{
    protected $noNeedLogin = ['*'];
    protected $noNeedRight = ['*'];


    /**
     * 轮播接口
     * @method GET
     * @param num 数量
     */
    public function Banner()
    {
        $number = $this->request->param('num',2);

        $list = Db::name('banner')
                ->where('status',1)
                ->limit($number)
                ->select();

        $this->success('获取成功',$list);
    }

    /**
     * 资讯获取
     * @ApiMethod (GET)
     * @param string $limit 结果数量
     */
    public function getInformation()
    {
        $params = $this->request->param();

        if (!empty($params)) {
            //验证参数，只能传数字
            $validate = new Vinfrom();
            if (!$validate->check($params)) {
                return $this->error($validate->getError());;
            }
        }

        $information = new Information();
        $result = $information->information($params['limit'] ?? 10);

        if ($result === false) {
            return $this->error('获取资讯失败,请稍后重试...');
        }

        $this->success('返回成功', $result);
    }

    /**
     * 股票搜索
     * @ApiMethod (GET)
     * @param string $like 用户输入
     */
    public function search()
    {
        $like = $this->request->param('like','');
        
        $newStock = new ModelSearch();
        $result = $newStock->stock($like);

        if ($result === false) {
            return $this->error('暂无数据,请稍后重试...');
        }

        $this->success('返回成功', $result);
    }

   
}
